package com.csii.adhoc.controller.info;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.dataready.TMgnDirPo;
import com.csii.adhoc.info.menu.service.MenuService;
import com.csii.adhoc.util.TreeUtils;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:菜单controller
 * @author:liuzhonghua
 * @create:2021.7.13
 */
@RequestMapping("/menu")
@RestController
@Slf4j
public class MenuController {
    @Resource
    private MenuService menuService;

    @OperationLog("新增菜单")
    @RequestMapping("addMenu")
    public APIResult<?> addMenu(@RequestBody TMgnDirPo tMgnDirPo){
        int add = menuService.addMenu(tMgnDirPo);
        if (add<=0){
            return APIResult.failResult(null,"新增失败");
        }else {
            return APIResult.successResult(null);
        }
    }
    @OperationLog("删除菜单")
    @RequestMapping("deleteMenu")
    public APIResult<?> deleteMenu(@RequestBody TMgnDirPo tMgnDirPo){
        int del = menuService.deleteMenu(tMgnDirPo);
        if (del<=0){
            return APIResult.failResult(null,"删除失败");
        }else {
            return APIResult.successResult("删除成功");
        }
    }
    @OperationLog("编辑菜单")
    @RequestMapping("editMenu")
    public APIResult<?> editMenu(@RequestBody TMgnDirPo tMgnDirPo){
        int upd = menuService.editMenu(tMgnDirPo);
        if (upd<=0){
            return APIResult.failResult(null,"修改失败");
        }else {
            return APIResult.successResult("修改成功");
        }
    }
    @OperationLog("上移下移")
    @RequestMapping("moveMenu")
    public APIResult<?> moveMenu(@RequestBody TMgnDirPo tMgnDirPo){
        menuService.moveMenu(tMgnDirPo);
        return APIResult.successResult("移动成功");
    }
    @RequestMapping("queryMenu")
    public APIResult<?> queryMenu(){
        List<TMgnDirPo> list = menuService.queryMenu();
        list = TreeUtils.createTree(list, "menuId","parentId","children");

        return APIResult.successResult(list);
    }
}
